namespace("org.jaxson.util.collections");

/**
 * HashMap implemented by wrapping object property key / value pairs
 * in an internal object.
 */
org.jaxson.util.collections.Map = function(){
	
	var mapObj = {};
	
	var that={
		
		size : function()
		{	
			return this.keys().elements.length;
		},
		
		isEmpty : function()
		{	
			return this.size() === 0;
		},
		
		keys : function()
		{	
			var result = new org.jaxson.util.collections.List();
			for( i in mapObj ){
				result.add( i );
			}
			return result;
		},
		
		values : function()
		{	
			var result = new org.jaxson.util.collections.List();
			for( i in mapObj ){
				result.add( mapObj[i] );
			}
			return result;
		},
		
		containsKey : function(key)
		{	
			return mapObj[key] !== undefined;
		},
		
		containsValue : function(value)
		{	
			var values = this.getValues();
			
			for( var i=0; i<values.size(); i++ ){
				if( values.get(i) === value ){
					return true;
				}
			}
			
			return false;
		},
		
		put : function(key, value)
		{	
			if( !key || !value || key === null || value === null ){
				throw "IllegalArgumentException: key and value must be defined and not null";
			}
			
			mapObj[key] = value;
		},
		
		get : function(key)
		{	
			return this.containsKey(key)?mapObj[key]:null;
		},
		
		remove : function(key)
		{	
			var result = this.containsKey(key)?this.get(key):null;
			delete mapObj[key];
			return result;
		},
		
		clear : function()
		{	
			var keys = this.keys();
			for( var i=0; i<keys.size(); i++ ){
				this.remove( keys.get(i) );
			}
		},
		
		toString : function()
		{	
			return "[object Map]";
		}
	};
	
	inherits( "JaxsonObject", "org.jaxson.util.collections.Map", that );
	return that;
};
